package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@RestController
@Slf4j
@RequestMapping("/admin/report")
@Api(tags = "报表统计管理")
public class ReportController {
    @Autowired
    private ReportService reportService;

    /**
     * 营业额统计
      * @param begin
     * @param end
     * @return
     */
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("接受到了参数{},{}",begin,end);
        TurnoverReportVO vo=reportService.turnoverStatistics(begin,end);

        return Result.success(vo);
    }


    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("接受到了参数{},{}",begin,end);
        UserReportVO vo=reportService.userStatistics(begin,end);

        return Result.success(vo);
    }


    /**
     * 订单数量统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("接受到了参数{},{}",begin,end);
        OrderReportVO vo=reportService.ordersStatistics(begin,end);
        return Result.success(vo);
    }


    /**
     * 销售 top10
     * @param begin
     * @param endd
     * @return
     */
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("接受到了参数{},{}",begin,end);
        SalesTop10ReportVO vo=reportService.top10(begin,end);
        return Result.success(vo);
    }


    /**
     * // 生成Excel 响应给前端
     * @param response :tomcat 创建的 ,HTTP 请求 原始的响应对象
     *                    response.getOutputStream();  可以给前端网页响应二进制数据
     * @return
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        reportService.export(response);
    }























}
